//
//  NSMutableAttributedString+CYHighlitText.m
//  cyydt
//
//  Created by chu on 2019/2/2.
//  Copyright © 2019 chu. All rights reserved.
//

#import "NSMutableAttributedString+CYHighlitText.h"

@implementation NSMutableAttributedString (CYHighlitText)
+(NSMutableAttributedString *)stringWithHighLightSubstring:(NSString *)totalString substring:(NSString *)substring{
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:totalString];
    NSString * copyTotalString = totalString;
    NSMutableString * replaceString = [NSMutableString stringWithCapacity:0];
    for (int i = 0; i < substring.length; i ++) {
        [replaceString appendString:@" "];
    }
    while ([copyTotalString rangeOfString:substring].location != NSNotFound) {
        NSRange range = [copyTotalString rangeOfString:substring];
        //颜色如果统一的话可写在这里，如果颜色根据内容在改变，可把颜色作为参数，调用方法的时候传入
        [attributedString addAttribute:NSForegroundColorAttributeName value:RGBOF(0xF43636) range:range];
        copyTotalString = [copyTotalString stringByReplacingCharactersInRange:range withString:replaceString];
    }
    return attributedString;
}

@end
